﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Data;

namespace EWMSWeb.ashx
{
    /// <summary>
    /// waitinstorage 的摘要说明
    /// </summary>
    public class waitinstorage : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";


            string iSortCol_0 = "";
            string sSortDir_0 = "";
            if (context.Request["iSortCol_0"] != null && context.Request["iSortCol_0"].ToString().Trim() != "")
            {
                iSortCol_0 = context.Request["iSortCol_0"].Trim();
                sSortDir_0 = context.Request["sSortDir_0"].Trim();
            }
            string json = "";

            string sEcho = "";
            int currpage = 0;
            int length = 0;
            string bcity = "";
            string gcity = "";
            string search = "";

            sEcho = context.Request["sEcho"];
            currpage = int.Parse(context.Request["iDisplayStart"]);
            length = int.Parse(context.Request["iDisplayLength"]);
            search = context.Request["sSearch"];
            bcity = HttpUtility.UrlDecode(context.Request["bcity"]);
            gcity = HttpUtility.UrlDecode(context.Request["gcity"]);
            string rtype = context.Request["rtype"];
            switch (rtype)
            {
                case "ins":
                    //待入库
                    json = GetInsList(sEcho, bcity, gcity, currpage, length, search);
                    break;
                case "wrl":
                    //待称重
                    json = GetWrlList(sEcho, bcity, gcity, currpage, length, search);
                    break;
                default:
                    break;
            }

            context.Response.Write(json);
        }

        /// <summary>
        /// 待入库
        /// </summary>
        /// <param name="sEcho"></param>
        /// <param name="bcity"></param>
        /// <param name="gcity"></param>
        /// <param name="currpage"></param>
        /// <param name="length"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        public string GetInsList(string sEcho, string bcity, string gcity, int currpage, int length, string search)
        {
            int totalRecord;
            DataLib.BLL.InStorage bll = new DataLib.BLL.InStorage();
            DataTable dt = bll.GetWatiInsByStore(currpage, length, out totalRecord, search, bcity, gcity, null, null, "admin");
            DataTablesModelWI m = new DataTablesModelWI();
            m.sEcho = sEcho;
            m.iTotalRecords = totalRecord;
            m.iTotalDisplayRecords = m.iTotalRecords;
            List<WIModel> l = new List<WIModel>();
            foreach (DataRow dr in dt.Rows)
            {
                WIModel mins = new WIModel();
                if (dr["rowno"] != null)
                    mins.rowno = int.Parse(dr["rowno"].ToString());
                if (dr["outs_vload"] != null)
                    mins.outs_vload = dr["outs_vload"].ToString();
                if (dr["indepart"] != null)
                    mins.indepart = dr["indepart"].ToString();
                if (dr["outdepart"] != null)
                    mins.outdepart = dr["outdepart"].ToString();
                if (dr["indepart"] != null)
                    mins.indepart = dr["indepart"].ToString();
                if (dr["outdepart"] != null)
                    mins.outdepart = dr["outdepart"].ToString();
                if (dr["load_totalweight"] != null)
                
                    mins.load_totalweight = dr["load_totalweight"].ToString();
                
                if (dr["load_finishweigth"] != null)
                    mins.load_finishweigth =dr["load_finishweigth"].ToString(); 
                if (dr["load_waitweight"] != null)
                    mins.load_waitweight = dr["load_waitweight"].ToString();
                if (dr["outs_toalweight"] != null)
                    mins.outs_toalweight = dr["outs_toalweight"].ToString();
                if (dr["outs_finishweight"] != null)
                    mins.outs_finishweight = dr["outs_finishweight"].ToString();
                if (dr["outs_waitweight"] != null)
                    mins.outs_waitweight = dr["outs_waitweight"].ToString();
                if (dr["ins_date"] != null && dr["ins_date"].ToString().Trim() != "" && dr["ins_date"].ToString().Trim() != "--")
                    mins.ins_date = DateTime.Parse(dr["ins_date"].ToString()).ToString("yyyy-MM-dd");
                if (dr["outs_no"] != null)
                    mins.outs_no = dr["outs_no"].ToString();
                if (dr["ins_no"] != null)
                    mins.ins_no = dr["ins_no"].ToString();
                if (dr["outStorageDate"] != null && dr["outStorageDate"].ToString().Trim() != "" && dr["outStorageDate"].ToString().Trim() != "--")
                    //mins.outStorageDate = DateTime.Parse(dr["outStorageDate"].ToString()).ToString("yyyy-MM-dd hh:mm:ss");
                    mins.outStorageDate = DateTime.Parse(dr["outStorageDate"].ToString()).ToString("yyyy-MM-dd");
                if (dr["outstatus"] != null && dr["outtype"] != null)
                {

                    string str = "";
                    string outstatus = dr["outstatus"].ToString();
                    string outtype = dr["outtype"].ToString();

                    if (outstatus == "2")
                    {
                        str += "移库派车中";
                    }
                    else if (outstatus == "3")
                    {
                        str += "移库称重中";
                    }
                    else
                    {
                        string txt = "";
                        switch (outtype)
                        {
                            case "1":
                                txt = "拆解入库";
                                break;
                            case "2":
                                txt = "分类入库";
                                break;
                            case "31":
                                txt = "生产审核";
                                break;
                            case "5":
                                txt = "移库入库";
                                break;
                            case "6":
                                txt = "直销入库";
                                break;
                            default:
                                break;
                        }


                        if (outtype == "6")
                        {
                            str += "<a href='InStorage.aspx?cjoid=" + dr["id"].ToString() + "'>" + txt + "</a>";
                        }
                        else if (outtype == "31")
                        {

                            DataLib.BLL.Productions p = new DataLib.BLL.Productions();
                            DataLib.Model.InStorage model = p.GetRsidByOutid(int.Parse(dr["outs_id"].ToString()));
                            if (model != null)
                                str += "<a href='/Production/ProduceAudit.aspx?insid=" + model.id.ToString() + "'>" + txt + "</a>";
                        }
                        else
                        {
                            str += "<a href='InStorage.aspx?outid=" + dr["outs_id"].ToString() + "'>" + txt + "</a>";
                        }



                    }


                    mins.operurl = str;
                }
                l.Add(mins);
            }
            m.aaData = l;
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string json = serializer.Serialize(m);
            return json;

        }

        /// <summary>
        /// 待称重
        /// </summary>
        /// <param name="sEcho"></param>
        /// <param name="bcity"></param>
        /// <param name="gcity"></param>
        /// <param name="currpage"></param>
        /// <param name="length"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        public string GetWrlList(string sEcho, string bcity, string gcity, int currpage, int length, string search)
        {
            int totalRecord;
            DataLib.BLL.CJOItem bll = new DataLib.BLL.CJOItem();
            DataTable dt = bll.GetWatiInsByStore(currpage, length, out totalRecord, search, bcity, gcity, null, null, "admin");
            DTWRLPage mpage = new DTWRLPage();
            mpage.sEcho = sEcho;
            mpage.iTotalRecords = totalRecord;
            mpage.iTotalDisplayRecords = mpage.iTotalRecords;
            List<WRLModel> l = new List<WRLModel>();
            foreach (DataRow dr in dt.Rows)
            {
                WRLModel m = new WRLModel();
                if (dr["rowno"] != null)
                    m.rowno = int.Parse(dr["rowno"].ToString());
                if (dr["loadnum"] != null)
                    m.loadnum = dr["loadnum"].ToString();
                if (dr["cjono"] != null)
                    m.cjono = dr["cjono"].ToString();
                if (dr["bcity"] != null)
                    m.bcity = dr["bcity"].ToString();
                if (dr["gcity"] != null)
                    m.gcity = dr["gcity"].ToString();
                if (dr["cjoid"] != null && dr["cjoid"].ToString() != "0")
                {
                    if (dr["cumcode"] != null)
                        m.cumcode = dr["cumcode"].ToString();
                    if (dr["cumname"] != null)
                        m.cumname = dr["cumname"].ToString();
                    if (dr["islocal"] != null)
                    {
                        if (dr["islocal"].ToString() == "1")
                        {
                            m.operurl = "<a href='InWrl.aspx?cjoid=" + dr["cjoid"].ToString() + "'>本地收集</a>";
                        }
                        else
                        {
                            m.operurl = "<a href='InWrl.aspx?cjoid=" + dr["cjoid"].ToString() + "'>异地收集</a>";
                        }
                    }
                }
                else
                {
                    if (dr["outid"] != null && dr["outid"].ToString() != "0")
                    {
                        int outid = int.Parse(dr["outid"].ToString());
                        DataLib.BLL.OutStorage bouts = new DataLib.BLL.OutStorage();
                        DataLib.Model.OutStorage model = new DataLib.Model.OutStorage();
                        model = bouts.GetModel(outid);

                        if (model.VLoadNum.Substring(0, 2) == "86")
                        {

                            DataLib.Model.LoadNum lmodel = new DataLib.BLL.LoadNum().GetLoadID(model.VLoadNum);
                            m.cumcode = lmodel.cumcode;
                            m.cumname = lmodel.cumname;


                        }
                        else
                        {
                            DataLib.BLL.Vload vload = new DataLib.BLL.Vload();
                            DataLib.Model.Vload mvload = vload.GetModelByOutID((int)model.id);
                            m.cumcode = mvload.inscumname;
                            m.cumname = mvload.inscumname;
                        }
                        m.operurl = "<a href='InWrl.aspx?outid=" + outid.ToString() + "'>移库收集</a>";


                    }
                }

                if (dr["peculiarrequire"] != null)
                {
                    m.peculiarrequire = "<span title='";
                    m.peculiarrequire += dr["peculiarrequire"].ToString();
                    m.peculiarrequire += "'>";
                    m.peculiarrequire += GetSubString(dr["peculiarrequire"].ToString(),5);
                    m.peculiarrequire += "</span>";
                }

                if(dr["numberestimate"]!=null)
                {
                    m.numberestimate = dr["numberestimate"].ToString();
                }
                if (dr["vehiclor"] != null)
                {
                    m.vehiclor = "<span title='";
                    m.vehiclor += dr["vehiclor"].ToString();
                    m.vehiclor += "'>";
                    m.vehiclor += GetSubString(dr["vehiclor"].ToString(), 5);
                    m.vehiclor += "</span>";
                }
                if (dr["plateNo"] != null)
                {
                    m.plateNo = "<span title='";
                    m.plateNo += dr["plateNo"].ToString();
                    m.plateNo += "'>";
                    m.plateNo += GetSubString(dr["plateNo"].ToString(), 5);
                    m.plateNo += "</span>";
                }
                if(dr["wrldate"]!=null)
                {
                    m.wrldate = DateTime.Parse(dr["wrldate"].ToString()).ToString("yyyy-MM--dd");
                }
                
                l.Add(m);
            }
            mpage.aaData = l;
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string json = serializer.Serialize(mpage);
            return json;

        }

        /// <summary>
        /// 获取字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string GetSubString(string str, int len)
        {
            string strreturn = "";
            if (str.Length > len)
            {
                strreturn = str.Substring(0, len) + "...";
            }
            else
            {
                strreturn = str;
            }
            return strreturn;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }


    #region 待入库
    [Serializable]
    public class DataTablesModelWI
    {
        private string _sEcho;
        private int _iTotalRecords;
        private List<WIModel> _aaData;
        private int _iTotalDisplayRecords;

        /// <summary>
        /// 行号
        /// </summary>
        public string sEcho
        {
            get { return _sEcho; }
            set { _sEcho = value; }
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int iTotalRecords
        {
            get { return _iTotalRecords; }
            set { _iTotalRecords = value; }
        }

        /// <summary>
        /// 过滤后的总记录数
        /// </summary>
        public int iTotalDisplayRecords
        {
            get { return _iTotalDisplayRecords; }
            set { _iTotalDisplayRecords = value; }
        }


        //数据集
        public List<WIModel> aaData
        {
            get { return _aaData; }
            set { _aaData = value; }
        }
    }


    

    [Serializable]
    public class WIModel
    {
        private int _rowno;
        private string _outs_vload;
        private string _indepart;
        private string _outdepart;
        private string _load_totalweight;
        private string _load_finishweigth;
        private string _load_waitweight;
        private string _outs_no;
        private string _ins_no;
        private string _outs_toalweight;
        private string _outs_finishweight;
        private string _outs_waitweight;
        private string _ins_date;
        private string _outStorageDate;
        private string _operurl;

        /// <summary>
        /// 行号
        /// </summary>
        public int rowno
        {
            get { return _rowno; }
            set { _rowno = value; }
        }

        /// <summary>
        /// 虚拟load
        /// </summary>
        public string outs_vload
        {
            get { return _outs_vload; }
            set { _outs_vload = value; }
        }


        /// <summary>
        /// 归属地
        /// </summary>
        public string indepart
        {
            get { return _indepart; }
            set { _indepart = value; }
        }

        /// <summary>
        /// 处理地
        /// </summary>
        public string outdepart
        {
            get { return _outdepart; }
            set { _outdepart = value; }
        }

        /// <summary>
        /// Load总重量
        /// </summary>
        public string load_totalweight
        {
            get { return _load_totalweight; }
            set { _load_totalweight = value; }
        }

        /// <summary>
        /// Load已重量
        /// </summary>
        public string load_finishweigth
        {
            get { return _load_finishweigth; }
            set { _load_finishweigth = value; }
        }


        /// <summary>
        /// Load未入库重量
        /// </summary>
        public string load_waitweight
        {
            get { return _load_waitweight; }
            set { _load_waitweight = value; }
        }


        /// <summary>
        /// 出库单号
        /// </summary>
        public string outs_no
        {
            get { return _outs_no; }
            set { _outs_no = value; }
        }

        /// <summary>
        /// 入库单号
        /// </summary>
        public string ins_no
        {
            get { return _ins_no; }
            set { _ins_no = value; }
        }

        /// <summary>
        /// 出库单总重量
        /// </summary>
        public string outs_toalweight
        {
            get { return _outs_toalweight; }
            set { _outs_toalweight = value; }
        }

        /// <summary>
        /// 出库单已入库
        /// </summary>
        public string outs_finishweight
        {
            get { return _outs_finishweight; }
            set { _outs_finishweight = value; }
        }

        /// <summary>
        /// 出库单未入库
        /// </summary>
        public string outs_waitweight
        {
            get { return _outs_waitweight; }
            set { _outs_waitweight = value; }
        }

        /// <summary>
        /// 入库时间
        /// </summary>
        public string ins_date
        {
            get { return _ins_date; }
            set { _ins_date = value; }
        }
    


        /// <summary>
        /// 出库时间
        /// </summary>
        public string outStorageDate
        {
            get { return _outStorageDate; }
            set { _outStorageDate = value; }
        }

        /// <summary>
        /// 操作URL
        /// </summary>
        public string operurl
        {
            get { return _operurl; }
            set { _operurl = value; }
        }
    }
    #endregion

    #region 待称重
    [Serializable]
    public class DTWRLPage
    {
        private string _sEcho;
        private int _iTotalRecords;
        private List<WRLModel> _aaData;
        private int _iTotalDisplayRecords;

        /// <summary>
        /// 行号
        /// </summary>
        public string sEcho
        {
            get { return _sEcho; }
            set { _sEcho = value; }
        }

        /// <summary>
        /// 总记录数
        /// </summary>
        public int iTotalRecords
        {
            get { return _iTotalRecords; }
            set { _iTotalRecords = value; }
        }

        /// <summary>
        /// 过滤后的总记录数
        /// </summary>
        public int iTotalDisplayRecords
        {
            get { return _iTotalDisplayRecords; }
            set { _iTotalDisplayRecords = value; }
        }


        //数据集
        public List<WRLModel> aaData
        {
            get { return _aaData; }
            set { _aaData = value; }
        }
    }




    [Serializable]
    public class WRLModel
    {
        private int _rowno;
        private string _loadnum;
        private string _cjono;
        private string _bicty;
        private string _gcity;
        private string _cumcode;
        private string _cumname;
        private string _peculiarrequire;
        private string _vehiclor;
        private string _plateNo;
        private string _wrldate;
        private string _operurl;
        private string _numberestimate;

        /// <summary>
        /// 行号
        /// </summary>
        public int rowno
        {
            get { return _rowno; }
            set { _rowno = value; }
        }

        /// <summary>
        /// load号
        /// </summary>
        public string loadnum
        {
            get { return _loadnum; }
            set { _loadnum = value; }
        }

        /// <summary>
        /// cjo号
        /// </summary>
        public string cjono
        {
            get { return _cjono; }
            set { _cjono = value; }
        }

        /// <summary>
        /// 归属地
        /// </summary>
        public string bcity
        {
            get { return _bicty; }
            set { _bicty = value; }
        }

        /// <summary>
        /// 处理地
        /// </summary>
        public string gcity
        {
            get { return _gcity; }
            set { _gcity = value; }
        }

        /// <summary>
        /// 客户编码
        /// </summary>
        public string cumcode
        {
            get { return _cumcode; }
            set { _cumcode = value; }
        }


        /// <summary>
        /// 客户名称
        /// </summary>
        public string cumname
        {
            get { return _cumname; }
            set { _cumname = value; }
        }

        /// <summary>
        /// 特殊要求
        /// </summary>
        public string peculiarrequire
        {
            get { return _peculiarrequire; }
            set { _peculiarrequire = value; }
        }

        /// <summary>
        /// 工人
        /// </summary>
        public string vehiclor
        {
            get { return _vehiclor; }
            set { _vehiclor = value; }
        }

        /// <summary>
        /// 车牌号
        /// </summary>
        public string plateNo
        {
            get { return _plateNo; }
            set { _plateNo = value; }
        }



        /// <summary>
        /// 收集日期
        /// </summary>
        public string wrldate
        {
            get { return _wrldate; }
            set { _wrldate = value; }
        }

        /// <summary>
        /// 操作URL
        /// </summary>
        public string operurl
        {
            get { return _operurl; }
            set { _operurl = value; }
        }

        /// <summary>
        /// 货物件数
        /// </summary>
        public string numberestimate
        {
            get { return _numberestimate; }
            set { _numberestimate = value; }
        }
    }
    #endregion
}